﻿using Sidvall.Net.Mime;
using Sidvall.Serialization;

namespace Sidvall.Net
{
    public class HttpClientSettings
    {
        #region Public Members

        public string BaseAddress { get; set; }
        public long? MaxRequestContentBufferSize { get; set; }
        public bool AllowAutoRedirect { get; set; }
        public bool? PreAuthenticate { get; set; }
        public bool? UseDefaultCredentials { get; set; }
        public LanguagePolicy LanguagePolicy { get; set; }
        public System.TimeSpan? Timeout { get; set; }
        public RangeHeaderValue Range { get; set; }
        public AuthenticationHeader AuthenticationHeader { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public MediaTypeHeaderCollection ResponseTypes { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public LanguageHeaderCollection LanguageHeaders { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public NameValueHeaderValueCollection CustomHeaders { get; set; }
        public ProxySettings ProxySettings { get; set; }

        #region Copy

        public HttpClientSettings Copy()
        {
            var item = new HttpClientSettings()
            {
                BaseAddress = this.BaseAddress,
                MaxRequestContentBufferSize = this.MaxRequestContentBufferSize,
                AllowAutoRedirect = this.AllowAutoRedirect,
                PreAuthenticate = this.PreAuthenticate,
                UseDefaultCredentials = this.UseDefaultCredentials,
                LanguagePolicy = this.LanguagePolicy,
                Timeout = this.Timeout,
            };
            if (this.Range != null)
                item.Range = this.Range.Copy();
            if (this.AuthenticationHeader != null)
                item.AuthenticationHeader = this.AuthenticationHeader.Copy();
            if (this.ResponseTypes != null)
                item.ResponseTypes = this.ResponseTypes.Copy();
            if (this.LanguageHeaders != null)
                item.LanguageHeaders = this.LanguageHeaders.Copy();
            if (this.CustomHeaders != null)
                item.CustomHeaders = this.CustomHeaders.Copy();
            if (this.ProxySettings != null)
                item.ProxySettings = this.ProxySettings.Copy();
            return item;
        }

        #endregion
        #region IsEqual

        public bool IsEqual(HttpClientSettings obj)
        {
            return IsEqual(this, obj);
        }

        #endregion
        #region IsEqual - Static

        public static bool IsEqual(HttpClientSettings item1, HttpClientSettings item2)
        {
            if (item1 == null)
            {
                if (item2 == null)
                    return true;
                else
                    return false;
            }
            else if (item2 == null)
                return false;
            if (!item1.BaseAddress.Equals(item2.BaseAddress, true, System.StringComparison.OrdinalIgnoreCase))
                return false;
            if (item1.MaxRequestContentBufferSize != item2.MaxRequestContentBufferSize)
                return false;
            if (item1.AllowAutoRedirect != item2.AllowAutoRedirect)
                return false;
            if (item1.PreAuthenticate != item2.PreAuthenticate)
                return false;
            if (item1.UseDefaultCredentials != item2.UseDefaultCredentials)
                return false;
            if (item1.LanguagePolicy != item2.LanguagePolicy)
                return false;
            if (item1.Timeout != item2.Timeout)
                return false;
            if (!RangeHeaderValue.IsEqual(item1.Range, item2.Range))
                return false;
            if (!AuthenticationHeader.IsEqual(item1.AuthenticationHeader, item2.AuthenticationHeader))
                return false;
            if (!MediaTypeHeaderCollection.IsEqual(item1.ResponseTypes, item2.ResponseTypes))
                return false;
            if (!LanguageHeaderCollection.IsEqual(item1.LanguageHeaders, item2.LanguageHeaders))
                return false;
            if (!NameValueHeaderValueCollection.IsEqual(item1.CustomHeaders, item2.CustomHeaders))
                return false;
            if (!ProxySettings.IsEqual(item1.ProxySettings, item2.ProxySettings))
                return false;
            return true;
        }

        #endregion

        #endregion
        #region Constructors

        public HttpClientSettings()
        {
            this.AllowAutoRedirect = true;
            this.PreAuthenticate = null;
            this.UseDefaultCredentials = null;
            this.LanguagePolicy = LanguagePolicy.LanguageHeadersOrCurrentCulture;
            this.ResponseTypes = new MediaTypeHeaderCollection();
            this.ResponseTypes.Add(MediaType.ApplicationJson);
            this.CustomHeaders = new NameValueHeaderValueCollection();
        }

        #endregion
    }
}
